package com.miya.demo.common.enums;

import com.miya.demo.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;

/**
 * 用户状态枚举
 *
 * @author Caixiaowei
 * @date 2022/07/08
 */
@Getter
@AllArgsConstructor
public enum UserStatusEnum {

    // 状态: 1-正常; 2-锁定; 3-过期; 4-禁用
    NORMAL(1, "正常"),
    LOCKING(2, "锁定"),
    EXPIRED(3, "过期"),
    DISABLE(4, "禁用");

    private final Integer code;
    private final String desc;

    private static final Set<UserStatusEnum> ALL = EnumSet.allOf(UserStatusEnum.class);

    public static UserStatusEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }

        return ALL.stream()
                .filter(o -> o.code.equals(code))
                .findAny().orElseThrow(() -> new BusinessException("用户状态: " + code + "未匹配到相关值！"));
    }

    public boolean is(Integer code) {
        return getCode().equals(code);
    }
}
